// const LinkedList = require('./LinkedList')

// let l = new LinkedList()

// // l.insert(0, 'head')
// // l.insert(1, 0)
// // l.insert(2, 1)
// // l.insert(3, 2)

// l.add(0)
// l.add(1)
// l.add(2)
// l.add(5)
// l.remove(5)

// console.log('-----')
// l.log()

const Dictionary = require('./Dictionary')

// let d = new Dictionary()

// d.add(1, 1)
// d.add(2, 2)
// d.add(3, 3)
// d.add(4, 4)

// d.remove(4)
// d.remove(1)

// console.log(d.showAll())


// ## 字典保存电话号码

/*
let phoneBook = new Dictionary()


phoneBook.add('张国荣', 15021142)
phoneBook.add('刘德华', 15021142)
phoneBook.add('马保国', 15021142)

phoneBook.remove('马保国')

console.log(phoneBook.showAll())
*/


// ## 字典统计单词 出现的个数

/*
let str = 'the brown fox jumped over the blue fox'
const countFn = (str) => {
    let arr = str.split(' ')
    let d = new Dictionary()
    for (let s of arr) {
        let item = d.find(s)
        if (item) {
            d.add(s, item + 1)
        } else {
            d.add(s, 1)
        }
    }
    console.log(d.showAll())
}
countFn(str)
*/

// ## 保存学生分数, 按照顺序展示(注意并列排名)

let studentList = [
    {name: '花泽香菜', score: 99},
    {name: '欧阳娜娜', score: 99},
    {name: '石原里美', score: 95},
    {name: '新垣结衣', score: 90},
    {name: '王冰冰', score: 98},
]

const sortScore = (studentList) => {
    let d = new Dictionary()
    for (let student of studentList) {
        let item = d.find(student.score)
        if (item) {
            item.push(student)
            d.add(student.score, item)
        } else {
            d.add(student.score, [ student ])
        }
    }

    let orderList = d.showAll().sort((first, next) => {
        return next.key - first.key
    })
    console.log(orderList)
    
    let i = 1
    for (let items of orderList) {
        let s = `第${i}名: `
        for (let item of items.value) {
            s += `${item.name} (${item.score})`
        }
        console.log(s)
        i++
    }
    // console.log(d.showAll())
}

sortScore(studentList)
